<?php
	#############################################################################################
	# Programmer: สมบูรณ์  กองลี																#
	# Email		: hugo8xx@hotmail.com.															#
	#############################################################################################
	
	if (!defined('BASE_ABS')) exit('No direct script access allowed');

	function splitPage($table,$filter="",$limit=20,$page="",$field="*", $count="") {
		$page	= (strip_tags($page)=="") ? 1 : $page;
		if($count!=""){
			$sql  = $count;
		} else {
			$sql = ($filter!="") ? "SELECT COUNT(*) AS all_row FROM $table WHERE $filter" : "SELECT COUNT(*) AS all_row FROM $table";
		}
		$query = mysql_query($sql) or die($sql);
		while($result = mysql_fetch_array($query)) {
			$all_row = $result['all_row'];
		}
		mysql_free_result($query);	
			
		if($all_row<$limit) { 
			$sql = "SELECT $field FROM $table"; 
			$sql .= ($filter!="") ? " WHERE $filter" : "";
			return array($sql,1);
		}

		$all_page = ceil($all_row/$limit);
		if($page>$all_page){
			$page = $all_page;
		}

		$sql = "SELECT $field FROM $table";
		$sql .= ($filter!="") ? " WHERE $filter" : "";
		$sql .= " LIMIT ".(($page-1)*$limit).",$limit";

		return array($sql,$all_page);
	}

	function fetchPage($all_page,$page,$link="",$pattern="?") {
		$cfg_limit_page_rang = 5;
		$script		= $link	? $link	: $_SERVER["REQUEST_URI"];
		$page		= $page	? $page	: 1;

		$first_link	= "{$link}{$pattern}p=1"; 
		$last_link	= "{$link}{$pattern}p=$all_page"; 

		if($all_page<2) {return;}

		$str_page			= array();
		$rang_page_stop		= ceil($page / $cfg_limit_page_rang) * $cfg_limit_page_rang;
		$rang_page_start	= $rang_page_stop - $cfg_limit_page_rang;
		if($rang_page_stop>$all_page){$rang_page_stop = $all_page;}
		
		if($rang_page_start >0 ){
			$url_link	= "{$link}{$pattern}p=$rang_page_start"; 
			$str_page_prv = $url_link;
		}
		$rang_page_start++;
		for($i=$rang_page_start;$i<=$rang_page_stop;$i++) {
			$url_link	= "{$link}{$pattern}p={$i}"; 
			$class_link = ($i==$page) ? "class='active'" : "";
			$str_page[]	= "<li><a href='$url_link' $class_link>$i</a></li>";	
		}
		$rang_page_stop++;

		if($rang_page_stop <= $all_page){
			$url_link	= "{$link}{$pattern}p=$rang_page_stop"; 
			$str_page_next = $url_link;
		}

		$str_page = implode($str_page);
		
		if(!$str_page_prv){$str_page_prv = '#';}
		if(!$str_page_next){$str_page_next = '#';}
		return $str_page = "<ol id=\"list-page\" class=\"list_page\">
		<li class=\"previous\"><a href=\"$str_page_prv\">&laquo; Previous</a></li> 
			$str_page
			<li class=\"next\"><a href=\"$str_page_next\">Next &raquo;</a></li>
			</ol>";
	}


	function _e($e='') {
		return  "--><p class='alert'>$e</p><!--";
	}

	function _u($str, $name='') {
		return  "begin $name -->$str<!-- end $name";
	}

	function add_magic_quotes( $array ) {
		foreach ( (array) $array as $k => $v ) {
			if ( is_array( $v ) ) {
				$array[$k] = add_magic_quotes( $v );
			} else {
				$array[$k] = addslashes( $v );
			}
		}
		return $array;
	}
    
	function stripslashes_deep($value) {
		if ( is_array($value) ) {
			$value = array_map('stripslashes_deep', $value);
		} elseif ( is_object($value) ) {
			$vars = get_object_vars( $value );
			foreach ($vars as $key=>$data) {
				$value->{$key} = stripslashes_deep( $data );
			}
		} else {
			$value = stripslashes($value);
		}

		return $value;
	}

	function magic_quotes() {
		// If already slashed, strip.
		if ( get_magic_quotes_gpc() ) {
			$_GET    = stripslashes_deep( $_GET    );
			$_POST   = stripslashes_deep( $_POST   );
			$_COOKIE = stripslashes_deep( $_COOKIE );
		}

		// Escape with wpdb.
		$_GET    = add_magic_quotes( $_GET    );
		$_POST   = add_magic_quotes( $_POST   );
		$_COOKIE = add_magic_quotes( $_COOKIE );
		$_SERVER = add_magic_quotes( $_SERVER );

		// Force REQUEST to be GET + POST.
		// $_REQUEST = array_merge( $_GET, $_POST );
	}
	magic_quotes();
	
	
	function redirect($url) {
		echo 
<<<ETO
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
	window.top.location='$url';
</script>
</head>

<body>
</body>
</html>
ETO;
	}

?>